﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using BrainTechLLC.FilterExtensions;
using BrainTechLLC.ImageOperators;
using BrainTechLLC.ImageOperatorsNonAForgeDependent;
using ImageAppViewModels;
using WpfImageDuplicates.Properties;

namespace WpfImageDuplicates
{
    /// <summary>
    /// Interaction logic for FilterView.xaml
    /// </summary>
    public partial class FilterView : UserControl
    {
        public event EventHandler<PropertyChangedEventArgs> PropertyChanged;

        public static readonly DependencyProperty FilterCollectionViewModelProperty =
            DependencyProperty.Register("FilterCollectionViewModel", typeof(FilterCollectionViewModel), typeof(FilterView), new PropertyMetadata(null, OnFilterCollectionViewModelPropertyChanged));

        public FilterCollectionViewModel FilterCollectionViewModel
        {
            get { return (FilterCollectionViewModel)GetValue(FilterCollectionViewModelProperty); }
            set { SetValue(FilterCollectionViewModelProperty, value); }
        }

        private static void OnFilterCollectionViewModelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            FilterView source = d as FilterView;
            FilterCollectionViewModel v = e.NewValue as FilterCollectionViewModel;
            source.UpdateFilterCollectionViewModel(v);
        }

        private void UpdateFilterCollectionViewModel(FilterCollectionViewModel viewModel)
        {
            FilterCollectionViewModel = viewModel;

            if (viewModel != null)
            {
                this.btnMoveLeft.CommandParameter = new DirectionAndFilter(Direction.Left, FilterViewModel, viewModel);
                this.btnMoveLeft.Command = viewModel.MoveFilterCommand;

                this.btnMoveRight.CommandParameter = new DirectionAndFilter(Direction.Right, FilterViewModel, viewModel);
                this.btnMoveRight.Command = viewModel.MoveFilterCommand;

                this.btnMoveLeftAll.CommandParameter = new DirectionAndFilter(Direction.Up, FilterViewModel, viewModel);
                this.btnMoveLeftAll.Command = viewModel.MoveFilterCommand;

                this.btnMoveRightAll.CommandParameter = new DirectionAndFilter(Direction.Down, FilterViewModel, viewModel);
                this.btnMoveRightAll.Command = viewModel.MoveFilterCommand;
            }
        }

        public static readonly DependencyProperty FilterViewModelProperty =
            DependencyProperty.Register("FilterViewModel", typeof(FilterViewModel), typeof(FilterView), new PropertyMetadata(null, OnFilterViewModelPropertyChanged));

        public FilterViewModel FilterViewModel
        {
            get { return (FilterViewModel)GetValue(FilterViewModelProperty); }
            set { SetValue(FilterViewModelProperty, value); }
        }

        private static void OnFilterViewModelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            FilterView source = d as FilterView;
            FilterViewModel v = e.NewValue as FilterViewModel;
            source.UpdateFilterViewModel(v);
        }

        private void UpdateFilterViewModel(FilterViewModel viewModel)
        {
            FilterViewModel = viewModel;
            this.DataContext = FilterViewModel;
        }

        public FilterView()
        {
            InitializeComponent();

            imgLeftAll.Source = Properties.Resources.Navigation_First3.CreateImageSourceFromBitmap(null);
            imgUp.Source = Properties.Resources.Navigation_Up3.CreateImageSourceFromBitmap(null);
            imgDown.Source = Properties.Resources.Navigation_Down3.CreateImageSourceFromBitmap(null);
            imgRightAll.Source = Properties.Resources.Navigation_Last3.CreateImageSourceFromBitmap(null);
        }

        private void FilterSettings(object sender, ExecutedRoutedEventArgs e)
        {
            PropertyGridWindow propGrid = new PropertyGridWindow() { Owner = Window1.MainWindow };
            propGrid.PropertyChanged += new EventHandler<System.ComponentModel.PropertyChangedEventArgs>(propGrid_PropertyChanged);
            propGrid.Loaded += new RoutedEventHandler(propGrid_Loaded);
            propGrid.Show();
            propGrid.Topmost = true;
        }

        void propGrid_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, e);
        }

        void propGrid_Loaded(object sender, RoutedEventArgs e)
        {
            PropertyGridWindow propGrid = sender as PropertyGridWindow;
            SingleFilter sf = FilterViewModel.Filter as SingleFilter;

            if (sf == null)
                throw new NotImplementedException();

            propGrid.Instance = sf.Filter;
        }

        private void UserControl_KeyDown(object sender, KeyEventArgs e)
        {
        }

        private void btnDisable_MouseEnter(object sender, MouseEventArgs e)
        {
            btnDisable.Content = (btnDisable.Content as string) + "*";
            if (FilterViewModel.Active)
            {
                FilterViewModel.TempNotActive = true;
            }
            else
            {
                FilterViewModel.TempActive = true;
            }
        }

        private void btnDisable_MouseLeave(object sender, MouseEventArgs e)
        {
            string s = btnDisable.Content as string;
            if (s.EndsWith("*"))
            {
                btnDisable.Content = s.Substring(0, s.Length - 1);
            }
            FilterViewModel.TempNotActive = false;
            FilterViewModel.TempActive = false;
        }

    }

    public class DirectionAndFilter
    {
        public Direction _dir;
        public FilterViewModel _filterVM;
        public FilterCollectionViewModel _filterCollectionVM;

        public DirectionAndFilter(Direction dir, FilterViewModel filterVM, FilterCollectionViewModel filterCollectionVM)
        {
            _filterCollectionVM = filterCollectionVM;
            _dir = dir;
            _filterVM = filterVM;
        }
    }    
}
